package com.ms.service.module.datacenterlite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.ms.game.lobby.GameLobbyProcesser;
import com.ms.service.module.datacenterlite.constant.DCLiteMessageType;
import com.xpec.c4.db.dao.PlayerKeepSeatDAO;
import com.xpec.c4.db.model.PlayerKeepSeatModel;
import com.xpec.c4.db.model.basic.BasicGameModel;
import com.xpec.c4.service.contexts.Contexts;

public class GameGetKeepSeatAyncCommand extends CentralAsyncCommand {

	// 全部
	public static final int TYPE_ALL = 0;

	// 保留資料
	public static final int TYPE_KEEPDATA = 1;

	// 離線保留&離線SPIN
	public static final int TYPE_OFFLINEALL = 2;

	/**
	 * 
	 */
	private static final long serialVersionUID = 634660806828512960L;

	/**
	 * 消息對應method type;
	 */
	public int methodType;

	/**
	 * 玩家guid
	 */
	public String playerGUID;

	/**
	 * 是否只讀取離線保留
	 */
	// public boolean onlyoffline;

	/**
	 * 需要讀取得資料類型
	 */
	public int datatype;

	/** 回呼參數 **/
	public Object[] parame;

	/** 玩家保留資訊 **/
	public ArrayList<GameKeepSeatData> keepdata = new ArrayList<GameKeepSeatData>();

	@Override
	public void asyncExecute() throws Exception {
		ConcurrentHashMap<String, GameKeepSeatAllData> keep = DataCenterLiteModule.getKeepSeat();

		String guid = playerGUID;

		GameKeepSeatAllData allData = keep.get(guid);

		boolean haveCacheData = true;
		if (allData == null) {
			// 如果取不 guid 對應的資料就 new 一個暫時的出來，讓同步鎖可以動作
			haveCacheData = false;
			allData = new GameKeepSeatAllData();
		}

		// 同步鎖
		synchronized (allData) {
			ArrayList<GameKeepSeatData> list = null;
			if (haveCacheData) {
				list = allData.getDataList();
			} else {
				PlayerKeepSeatModel model = PlayerKeepSeatDAO.getKeepSeatByGuid(guid);
				if (model != null) {
					list = parseJson(model.getContent(), model.getDb_id());
				}
			}

			if (list == null) // 此玩家無資料
			{
				return;
			}

			// 讀取資料類型
			switch (datatype) {
			case TYPE_OFFLINEALL: {
				for (GameKeepSeatData dd : list) {
					if (dd.getMachinestat() == BasicGameModel.OFFLINE_KEEP
							|| dd.getMachinestat() == BasicGameModel.OFFLINE_KEEP_SPIN) {
						keepdata.add(dd);
						break;
					}
				}
			}
				break;
			case TYPE_KEEPDATA: {
				for (GameKeepSeatData dd : list) {
					if (dd.getMachinestat() == BasicGameModel.KEEP) {
						keepdata.add(dd);
					}
				}
			}
				break;
			default: {
				keepdata = list;
			}
				break;
			}
		} // synchronized (allData) {
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		switch (methodType) {
			case DCLiteMessageType.LOBBY_GET_OFFLINEKEEPDATA: {
				GameLobbyProcesser.onCheckPlayerKeepDataBack(keepdata, userData);
			}
				break;
			case DCLiteMessageType.FISH_GET_KEEPDATA: {
				Contexts.getModule().getBasicprocesser().onGetKeepDataCallBack(keepdata, callbackMethod, userData);
			}break;
			}
	}

	/**
	 * 轉換保留資訊
	 * 
	 * @param value
	 */
	public ArrayList<GameKeepSeatData> parseJson(String value, String modeldbid) {
		ArrayList<GameKeepSeatData> list = new ArrayList<GameKeepSeatData>();
		if (value.equals("")) {
			return list;
		}

		HashMap data = (HashMap) JSONValue.parse(value);

		JSONArray keep = (JSONArray) data.get("keep");

		// 保留資訊
		for (Object o : keep) {
			JSONObject json = (JSONObject) o;

			GameKeepSeatData seat = new GameKeepSeatData();
			seat.setModeldbid(modeldbid);
			seat.setSeatid((String) json.get("seatid"));
			seat.setIsofflinekeep(((long) json.get("offlinekeep") == 1) ? true : false);
			seat.setGameName((String) json.get("game"));
			seat.setHalltype((String) json.get("hall"));
			seat.setMachineid((int) (long) json.get("machineid"));
			seat.setTimeout((long) json.get("timeout"));
			seat.setMachinestat((int) (long) json.get("machinestat"));

			list.add(seat);
		}
		return list;
	}
}
